<template>
  <div class="home" v-loading="loading">
    <div class="swiper-slide slide-one">
      <div class="page"></div>
    </div>
    <div class="swiper-slide slide-two">
      <div class="page">
        <h3>开设楼盘</h3>
        <p>Property</p>
      </div>
      <ul class="case-item">
        <li
          v-for="(item, index) in caseList"
          :key="index"
          :style="{ backgroundImage: `url(${getFirstImageUrl(item.content)})` }"
        >
          <NuxtLink class="text-decoration" :to="`/CaseDetails/${item.id}`">
            <div class="case-item-hover">
              <div class="group-title">
                <p class="hover-title" :title="item.title">{{ item.title }}</p>
                <div class="bottom"></div>
              </div>
              <div class="more">
                <span>MORE</span>
              </div>
            </div>
          </NuxtLink>
        </li>
      </ul>
    </div>
    <div class="swiper-slide slide-three">
      <div class="page">
        <h3>最新资讯</h3>
        <p>Latest News</p>
      </div>
      <div class="news-content">
        <div class="news-content-item" v-for="(news, i) in newsList" :key="i">
          <NuxtLink class="text-decoration" :to="`/NewsDetails/${news.id}`">
            <div
              class="item-img"
              :style="{
                backgroundImage: `url(${getFirstImageUrl(news.content)})`,
              }"
            ></div>
            <div class="item-content">
              <h3 :title="news.title">{{ news.title }}</h3>
            </div>
          </NuxtLink>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";

const loading = ref(true);
const caseList = ref([]);
const newsList = ref([]);

try {
  const { data: caseData } = await baseFetch("case/all");
  const { data: newsData } = await baseFetch("news/all");

  caseList.value = caseData?.value || [];
  newsList.value = newsData?.value || [];

  if (newsList.value?.length > 0) {
    newsList.value.sort((a, b) => {
      return new Date(b.createTime) - new Date(a.createTime);
    });
  }
} catch (error) {
  console.error("数据加载失败:", error);
} finally {
  loading.value = false;
}
</script>

<style lang="scss" scoped>
.home {
  .swiper-slide {
    padding: 6vh 0;
    font-size: 24px;
    display: flex;
    flex-direction: column;

    .page {
      text-align: center;
      overflow: hidden;

      p {
        font-size: 36px;
        font-weight: 300;
      }

      h3 {
        font-size: 56px;
        font-weight: 600;
      }

      @media screen and (max-width: 700px) {
        p {
          font-size: 20px;
        }

        h3 {
          font-size: 25px;
        }
      }
    }
  }

  .case-item {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-gap: 2px;
    margin: 0 auto;
    padding: 0;

    li {
      width: 30vw;
      height: 28vw;
      max-width: 300px;
      max-height: 280px;
      list-style: none;
      background-repeat: no-repeat;
      background-size: cover;
      background-position: center;
      background-origin: content-box;
      position: relative;
      overflow: hidden;

      &:hover {
        .case-item-hover {
          opacity: 1;
          transition: all 0.4s ease-in-out;
        }
      }
    }
  }

  @media screen and (min-width: 1800px) {
    .case-item {
      grid-template-columns: repeat(6, 1fr);

      li {
        width: 15vw;
        height: 14vw;
      }
    }
  }

  .case-item-hover {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    opacity: 0;
    overflow: hidden;
    background-color: rgba(225, 56, 52, 0.7);
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;

    .hover-title {
      padding: 0 10px;
      text-align: center;
      color: #fff;
      font-size: 18px;
      text-overflow: ellipsis;
      white-space: nowrap;
      font-weight: 400;
      overflow: hidden;
    }

    .bottom {
      border-bottom: 2px solid #fff;
      width: 60px;
      margin: 0 auto;
    }

    .more {
      width: 30%;
      min-width: 60px;
      padding: 5px 5px;
      margin: 0 auto;
      margin-bottom: 10px;
      border: 2px solid #fff;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      span {
        color: #fff;
        font-size: 20px;
      }
    }
  }

  .slide-one {
    background: url("../static/mbpy.webp") no-repeat center;
    background-size: 100% 100%;
    height: 45vw;
    justify-content: center;
    align-items: start;
    color: #000;
    padding: 0;
  }

  .slide-three {
    background: #f5f5f5;
  }

  .news-content {
    margin: 0 auto;

    &-item {
      background: transparent;
      width: 88vw;
      margin-bottom: 20px;

      .item-img {
        width: 88vw;
        height: 50vw;
        background-repeat: no-repeat;
        background-size: cover;
        background-position: center;
        background-origin: content-box;
      }

      .item-content {
        h3 {
          color: #000;
          margin: 5px;
          font-size: 16px;
          white-space: normal;
        }
      }
    }
  }

  @media screen and (min-width: 1000px) {
    .slide-one {
      height: 40vw;
    }

    .news-content {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      justify-items: center;
      grid-gap: 2vw;

      &-item {
        width: 28vw;
        margin: 0;

        .item-content {
          h3 {
            font-size: 16px;
          }
        }

        .item-img {
          width: 28vw;
          height: 26vw;
        }
      }
    }
  }
}

.text-decoration {
  text-decoration: none;
}
</style>
